package com.cyyz.tai.service;

import com.cyyz.tai.dao.PointDao;
import com.cyyz.tai.model.DashPoint;
import com.vteba.common.service.impl.CommonServiceImpl;
import com.vteba.tx.dao.Page;
import com.vteba.tx.dao.spi.SpringGenericDao;
import com.vteba.utils.id.ObjectId;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.transaction.Transactional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Named
@Transactional
public class PointService extends CommonServiceImpl<DashPoint, String> {

    private static final Logger LOGGER = LogManager.getLogger(PointService.class);

    private PointDao pointDao;

    @Inject
    @Override
    public void setSpringGenericDaoImpl(SpringGenericDao<DashPoint, String> pointDao) {
        this.pointDao = (PointDao) pointDao;
        this.springGenericDaoImpl = pointDao;
    }

    public void test() {
        DashPoint point = new DashPoint();
        point.setId(ObjectId.getId());
        point.setCreateDate(new Date());
        point.setStates(2);
        point.setLabel("test");
        point.setLat("122");
        point.setLon("33");

        String id = pointDao.save(point);

        point.setId(ObjectId.getId());
        int i = pointDao.persist(point);
        LOGGER.info("保存的id=[{}], i=[{}]", id, i);

        DashPoint dashPoint = pointDao.get("683d5ad60347db20490e47ab");
        int j = pointDao.deleteById("683d5adc0347db20490e47ac");

        int k = pointDao.delete(point);

        DashPoint query = new DashPoint();
        query.setLabel("test");
        query.setLat("122");
        List<DashPoint> dashPointList = pointDao.queryList(query);

        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("label", "test");
        List<DashPoint> dashPointList2 = pointDao.queryList(queryMap);

        String sql = "select * from dash_point where label = :label";
        List<DashPoint> dashPointList3 = pointDao.queryList(sql, queryMap);

        List<DashPoint> dashPointList4 = pointDao.queryList(sql, query);

        DashPoint update = new DashPoint();
        update.setId("683d5ad60347db20490e47ab");
        update.setLat("321");
        int l = pointDao.updateById(update);

        update.setId(null);
        update.setCreateDate(new Date());
        update.setLat("256");

        DashPoint where = new DashPoint();
        where.setLabel("test");
        where.setLat("122");
        int m = pointDao.updateBatch(update, where);

        String updateSQL = "update dash_point set create_date = :create_date where label = :label";

        DashPoint where2 = new DashPoint();
        where2.setCreateDate(new Date());
        where2.setLabel("test");
        int n = pointDao.updateBatch(updateSQL, where2);

        DashPoint setValue = new DashPoint();
        setValue.setCreateDate(new Date());
        setValue.setLon("56");

        Map<String, String> where3Map = new HashMap<>();
        where3Map.put("label", "test");
        int o = pointDao.updateBatch(setValue, where3Map);

        DashPoint unique = new DashPoint();
        unique.setId("683d5ad60347db20490e47ab");
        DashPoint un1 = pointDao.unique(unique);

        Map<String, String> uniqueMap = new HashMap<>();
        uniqueMap.put("id", "683d5ad60347db20490e47ab");
        DashPoint un2 = pointDao.unique(uniqueMap);

        DashPoint queryLst = new DashPoint();
        queryLst.setLabel("test");
        String queryLstSQL = "select id, create_date, label, lat, lon from dash_point where label = :label";
        List<DashPoint> lst2 = pointDao.query(queryLstSQL, queryLst);

        DashPoint params = new DashPoint();
        params.setLabel("test");

        Page<DashPoint> page = new Page<>();
        page.setPageNo(1);
        page.setPageSize(10);
        Page<DashPoint> page1 = pointDao.queryForPage(page, params);

        Page<DashPoint> page2 = pointDao.queryForPage(page, where3Map);

        Page<DashPoint> page3 = pointDao.queryPageList(page, queryLstSQL, params);

        Integer count = pointDao.countBy(params);
        Integer count2 = pointDao.countBy(queryMap);

        DashPoint delete2 = new DashPoint();
        delete2.setLon("321");
        int p = pointDao.deleteBatch(delete2);

        DashPoint delete3 = new DashPoint();
        delete3.setLon("56");
        delete3.setId("683d5fc42723e663cd063f11");
        String deleteSQL = "DELETE FROM DASH_POINT WHERE lon = :lon and id = :id";
        int q = pointDao.deleteBatch(deleteSQL, delete3);

        LOGGER.info("测试end");
    }
}
